package com.wukong.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/announcements")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    @GetMapping
    public String listAnnouncements(Model model) {
        List<Announcement> announcements = announcementService.getAllAnnouncements();
        model.addAttribute("announcements", announcements);
        return "announcement/list";
    }

    @GetMapping("/{id}")
    public String getAnnouncement(@PathVariable Long id, Model model) {
        Announcement announcement = announcementService.getAnnouncement(id);
        model.addAttribute("announcement", announcement);
        return "announcement/detail";
    }

    @PostMapping
    public String createAnnouncement(@ModelAttribute Announcement announcement) {
        announcementService.createAnnouncement(announcement);
        return "redirect:/announcements";
    }
}
